package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.model.City;


@Repository
public class CityDaoImpl {
	@Autowired
	SessionFactory sessionFactory;
	
	
	public List<City> getAllCities() {
		return sessionFactory.getCurrentSession().createQuery("from City").list();		
	}

	@SuppressWarnings("unchecked")
	public City getCityById(Integer id) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from City where id=  :id");
		 query.setParameter("id", id);
		 List<City>list = new ArrayList<City>();
		list =   query.list();
		if(list.size()>0){
			return list.get(0);
			
		}else{
			return new City();
			
		}
		 
		
	}
	
	@SuppressWarnings("unchecked")
	public List<City> getAllCityByCountryId(Integer countryId) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from City  where country.id = :countryId ");
		 query.setParameter("countryId", countryId);
	     return query.list();
		 
	}
	public City getCityByName(String name) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from City where name=  :name");
		 query.setParameter("name", name);
		 List<City>list = new ArrayList<City>();
		list =   query.list();
		if(list.size()>0){
			return list.get(0);
			
		}else{
			return null;
		}
	}
}
